package com.lightheart.sphr.xiaobinggan.module.home.adapter;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class BigImageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;
    private List<Integer> mItemIdList = new ArrayList<>();
    private int id = 0;
    private FragmentManager mFm;

    public BigImageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.mFm = fm;
        this.fragments = fragmentList;
        for (int i = 0; i < fragmentList.size(); i++) {
            mItemIdList.add(id++);
        }
    }

    public List<Fragment> getFragmentList() {
        return fragments;
    }

    public void setFragmentList(List<Fragment> fragmentList) {
        this.fragments = fragmentList;
    }

    public void delPage(int index) {
        fragments.remove(index);
        mItemIdList.remove(index);
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        if (object instanceof Fragment) {
            if (fragments.contains(object)) {
                return fragments.indexOf(object);
            } else {
                return POSITION_NONE;
            }

        }
        return super.getItemPosition(object);
    }

    @Override
    public long getItemId(int position) {
        return mItemIdList.get(position);
    }

}
